<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>派工单-申请质检</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12" style="margin-top: 50px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group" style="margin-right: 50px;">
                    <label>用户名：[[${user.name}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>部门：[[${user.deptName}]]</label>
                </div>
                <div class="form-group" style="margin-right: 50px;">
                    <label>车间：[[${user.branchName}]]</label>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <div class="form-inline">
                <div class="form-group">
                    <label for="select-prodNo">生产令编号</label>
                    <select id="select-prodNo" class="form-control" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-special">类型</label>
                    <select id="select-special" class="form-control select2" style="width: 140px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-part">部分</label>
                    <select id="select-part" class="form-control select2" style="width: 200px;text-align: center"></select>
                </div>
                <div class="form-group">
                    <label for="select-drawNo">图号</label>
                    <select id="select-drawNo" class="form-control select2" style="width: 300px;text-align: center"></select>
                </div>
            </div>
        </div>
        <div class="col-xs-12" style="margin-top: 20px;margin-left: 2%;margin-right: 2%;">
            <form id="queryForm" onsubmit="return false;">
                <div class="form-inline">
                    <div class="form-group">
                        <label for="query-barCode">二维码区域</label>
                        <input id="query-barCode" type="text" name="barCode" class="form-control"
                               placeholder="请扫描装配派工单二维码"
                               style="width: 550px;margin-right: 10px;" autocomplete="off" onkeyup="loadData()">
                        <button type="button" class="btn btn-success" id="btn-search" style="margin-bottom: 0px;">
                            <span class="Bold">搜索</span>
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <hr style="margin-left: 2%;margin-right: 2%;margin-top: 30px;">
    <div class="row">
        <div class="col-xs-12">
            <div style="margin-top: 15px;margin-left: 1%;margin-right: 1%;">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>

</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    let table;

    /** 申请质检 */
    function apply(obj, id, applyNum, index) {
        js.modal.confirm("确认申请质检吗？", function () {
            js.post({
                url: ctx + "assemble/apply/apply",
                data: {
                    id: id,
                    applyNum: applyNum
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table);
                    } else {
                        js.modal.warning(result.msg);
                        $.post(
                            ctx + 'assemble/apply/getNewMayApplyNum',
                            {
                                id: id
                            },
                            function (res) {
                                let mayApplyNum = res.data;
                                $(obj).parent().parent().find("input[name='applyNum']").attr('data-max', mayApplyNum);
                                saveData(index, 'mayApplyNum', mayApplyNum);
                            }
                        );
                    }
                }
            })
        })
    }

    /**
     * 检测回车
     */
    function loadData() {
        let ev = window.event || e;
        let keyCode = ev.keyCode ? ev.keyCode : ev.which ? ev.which : ev.charCode;
        if (keyCode == 13) {
            js.table.refresh(table);
        }
    }

    $(function () {

        /** 生产令号下拉选 */
        js.post({
            url: ctx + "assemble/apply/getRecProdNoList",
            data: {},
            success: function (result) {
                $("#select-prodNo").html('').select2();
                let select2Html = '<option value=""></option>';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                $("#select-prodNo").html(select2Html).select2();
            }
        });
        /** 项目编号下拉框选中 */
        $("#select-prodNo").on("select2:select", function () {
            getSpecials();
        });
        /** 类型下拉框选中 */
        $("#select-special").on("select2:select", function () {
            getParts();
        });
        /** 部分下拉框选中 */
        $("#select-part").on("select2:select", function () {
            getDrawNos();
        });
        /** 图号下拉框选中 */
        $("#select-drawNo").on("select2:select", function () {
            if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
            } else {
                $("#query-barCode").val('');
            }
            js.table.refresh(table);
        });

        $('#bootstrap-table').bootstrapTable('destroy');//这里必须要添加这个销毁，否则新增、修改、查看的切换可编辑列表中的数据可能加载出现问题。
        /** 表单数据绑定 */
        table = js.table.init({
            url: ctx + "assemble/apply/appList",
            showExport: false,
            pagination: false,
            columns: [
                {
                    title: '序号', field: 'id', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: "项目编码", field: 'proCode'},
                {title: "项目名称", field: 'proName'},
                {title: "类型", field: 'special'},
                {title: "部分", field: 'part'},
                {title: "装配图号", field: 'drawNo'},
                {title: "装配名称", field: 'drawName'},
                {title: "工艺路线", field: 'route'},
                {title: "装配数量", field: 'drawNum'},
                {
                    title: "申请质检数量", field: 'applyNum',
                    formatter: function (value, row, index) {
                        if (value != null) {
                            return '<button onclick="add(this)" class="btn btn-success btn-xs"><i class="fa fa-plus"></i></button><input name="applyNum" oninput="formatNumber(' + index + ',this)" onchange="formatNumber(' + index + ',this)" data-max="' + row.mayApplyNum + '" data-id = "' + row.id + '" style="width: 50px" value="' + row.applyNum + '" /><button onclick="reduce(this)" class="btn btn-danger btn-xs" ><i class="fa fa-minus"></i></button>';
                        } else {
                            return '--';
                        }
                    }
                },
                {
                    title: "路线状态", field: 'status',
                    formatter: function (value, row) {
                        if (value === 1) {
                            return '<span class="badge label-inverse">未开始</span>';
                        } else if (value === 2) {
                            return '<span class="badge label-warning">执行中</span>';
                        } else if (value === 3) {
                            return '<span class="badge label-warning">部分转序</span>';
                        } else if (value === 4) {
                            return '<span class="badge label-success">全部转序</span>';
                        }
                    }
                },
                {
                    title: "操作", width: '70',
                    formatter: function (value, row, index) {
                        let elements = new Array();
                        if (hasP('asseOrd:testings:commit')) {
                            if ((row.status === 1 || row.status === 2 || row.status === 3) && row.applyNum > 0) {
                                elements.push('<button class="btn btn-info btn-xs" onclick="apply(this,' + row.id + ',' + row.applyNum + ',' + index + ')">申请质检</button>');
                            }
                        }
                        return elements.join('');
                    }
                }
            ]
        });

        /** 点击查询 */
        $("#btn-search").click(function () {

            /** 校验二维码有效性 */
            var mm = $("#query-barCode").val().split("|");
            if (mm.length != 4) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            if (mm[3] != 2) {
                js.modal.warning("二维码无效，请核对后再次扫描");
                return;
            }
            js.table.search(table);
        });

    })

    /** 保存数据并更新到表格UI页面 */
    function saveData(index, field, value) {
        table.bootstrapTable('updateCell', {
            index: index,       //行索引
            field: field,       //列名
            value: value        //cell值
        })
    }

    /**
     * 增加
     */
    function add(obj) {
        let next = $(obj).next("input[name='applyNum']");
        next.val(parseInt(next.val()) + 1);
        next.attr('style', 'width: 50px');
        next.change();
    }

    /**
     * 减少
     */
    function reduce(obj) {
        let prev = $(obj).prev("input[name='applyNum']");
        if (parseInt(prev.val()) > 0) {
            prev.val(parseInt(prev.val()) - 1);
            prev.attr('style', 'width: 50px');
            prev.change();
        }
    }

    /**
     * 验证输入有效的整数
     */
    function formatNumber(index, obj) {
        obj.value = obj.value.replace(/[^\d]/g, "");
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\*$/, '$1$2.$3');//只能输入六个小数
        if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
            obj.value = obj.value;
        }
        //如果上去就写- 自动替换
        if (obj.value.indexOf("-") >= 0) {
            obj.value = obj.value.replace('-', "");
        }
        if (obj.value == '00') {
            obj.value = obj.value = '0';
        }
        //如果输入023直接变成23;自动去除整数部分的0
        if (obj.value.length > 2 && obj.value.indexOf(".") < 0) {
            if (obj.value.indexOf("0") == 0) {
                obj.value = obj.value.substr(1, obj.value.length - 1);
            }
        }
        getNewMayApplyNum(index, $(obj).attr('data-id'), obj);
    }

    /** 获取最新的可申请数量 */
    function getNewMayApplyNum(index, id, obj) {
        $.post(
            ctx + 'assemble/apply/getNewMayApplyNum',
            {
                id: id
            },
            function (res) {
                let mayApplyNum = res.data;
                $(obj).attr('data-max', mayApplyNum);
                saveData(index, 'mayApplyNum', mayApplyNum);
                if (parseInt($(obj).val()) > parseInt($(obj).attr('data-max'))) {
                    $(obj).val($(obj).attr('data-max'));
                }
                saveData(index, 'applyNum', $(obj).val());
            }
        );
    }

    /** 获取类型列表 */
    function getSpecials() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoSpecialList",
            data: {prodNo: $("#select-prodNo").val()},
            success: function (result) {
                $("#select-special").empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-special").html(select2Html).select2();
                    $("#select-special").val(result.data[0].id).select2();
                }
                getParts();
            }
        });
    }
    /** 获取部分列表 */
    function getParts() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoPartList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val()
            },
            success: function (result) {
                $("#select-part").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-part").html(select2Html).select2();
                    $("#select-part").val(result.data[0].id).select2();
                }
                getDrawNos();
            }
        });
    }
    /** 获取图号列表 */
    function getDrawNos() {
        js.post({
            url: ctx + "assemble/apply/getRecProdNoDrawNoList",
            data: {
                prodNo: $("#select-prodNo").val(),
                special: $("#select-special").val(),
                partId: $("#select-part").val()
            },
            success: function (result) {
                $("#select-drawNo").html('').empty();
                let select2Html = '';
                for (let i = 0; i < result.data.length; i++) {
                    select2Html += '<option value="' + result.data[i].id + '">' + result.data[i].text + '</option>';
                }
                if (result.data.length > 0) {
                    $("#select-drawNo").html(select2Html).select2();
                    $("#select-drawNo").val(result.data[0].id).select2();
                }
                if ($("#select-prodNo").val() != null && $("#select-prodNo").val() != '' && $("#select-drawNo").val() != null && $("#select-drawNo").val() != '' && $("#select-part").val() != null && $("#select-part").val() != '') {
                    $("#query-barCode").val($("#select-prodNo").val() + '|' + $("#select-drawNo").val() + '|' + $("#select-part").val() + '|2');
                } else {
                    $("#query-barCode").val('');
                }
                js.table.refresh(table);
            }
        });
    }


</script>
</body>
</html>
